/**
 * Created by xstneton 2018/4/29.
 */
import React from 'react';
import { Icon, Menu } from 'antd';
import { connect } from 'react-redux'
import { changeTabNav, addTabNav } from '@/store/reducers/common/action'
import { getMenu} from '@/api/Api.js'
import { Link } from 'react-keeper';
import '../../style/public/sidebar.css';


const SubMenu = Menu.SubMenu;

const mapStateToProps = (state, ownProps) => ({
	menuList: state.common.menuList,
	current: state.common.activeKey,
	// tabNavList: state.common.tabNavList,
})

const mapDispatchToProps = (dispatch, ownProps) => ({
	changeTabNav: key => dispatch(changeTabNav(key)),
	addTabNav: (key, title, keyPath) => dispatch(addTabNav(key, title, keyPath))
})

class Sidebar extends React.Component {
	// constructor(props) {
	// 	super(props);
	// }

	// componentWillReceiveProps(nextProps) {
	// 	// console.log(nextProps);
	// 	// this.onCollapse(nextProps.collapsed);
	// 	// this.setMenuOpen(nextProps)
	// }

	componentWillMount() {
		this.props.changeTabNav(this.props.current);
	}

	handleClick = (e) => {
		this.props.addTabNav(e.key, '', e.keyPath);
	}

	componentDidMount() {
		getMenu().then(result => {
			// this.setState({todolistLoading: false});
			// console.log(result, 111)
		})
	}

	render() {
		return (
			<Menu theme="dark" onClick={this.handleClick} defaultSelectedKeys={['/']} selectedKeys={[this.props.current]} mode="inline">
				<Menu.Item key="/">
					<Link to="/">
						<Icon type="home" />
						<span>首页</span>
					</Link>
				</Menu.Item>
				{this.props.menuList.map((menu) => {
					return (!menu.child) ? (
						<Menu.Item key={menu.url}>
							<Link to={'/admin'+menu.url}>
								<Icon type={menu.icon} />
								<span>{menu.name}</span>
							</Link>
						</Menu.Item>
					) : (
						<SubMenu
							key={menu.url}
							title={<span><Icon type={menu.icon} /><span>{menu.name}</span></span>}
						>
							{menu.child.map(item => {
								return <Menu.Item key={item.url}><Link to={'/admin'+item.url}>{item.name}</Link></Menu.Item>
							})}
						</SubMenu>
					)
				})}
			</Menu>
		);
	}
}

export default connect(
	mapStateToProps,
	mapDispatchToProps
)(Sidebar);